package com.cmw.auntapp.adapter;

import android.os.Handler;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;

import com.cmw.auntapp.adapter.holder.BaseViewHolder;
import com.cmw.auntapp.adapter.holder.ChatAcceptViewHolder;
import com.cmw.auntapp.adapter.holder.ChatSendViewHolder;
import com.cmw.auntapp.model.im.MessageInfo;
import com.google.android.gms.common.util.CollectionUtils;

import java.util.ArrayList;
import java.util.List;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

import static com.cmw.auntapp.constant.Constant.CHAT_ITEM_TYPE_LEFT;
import static com.cmw.auntapp.constant.Constant.CHAT_ITEM_TYPE_RIGHT;

/**
 * @desc: IM Chat 消息适配类
 * @author: chengmingwei
 * @date: 2021/8/27 7:26 PM
 * @contact: phone(微信同号):13922111489 / email: 340360491@qq.com
 */
public class ChatAdapter extends RecyclerView.Adapter<BaseViewHolder> {

    private OnItemClickListener onItemClickListener;
    public Handler handler;
    private List<MessageInfo> messageInfoList;

    public ChatAdapter(List<MessageInfo> messageInfoList) {
        this.handler = new Handler();
        this.messageInfoList = messageInfoList;
    }

    public void addOnItemClickListener(OnItemClickListener onItemClickListener) {
        this.onItemClickListener = onItemClickListener;
    }

    @NonNull
    @Override
    public BaseViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        BaseViewHolder viewHolder = null;
        switch (viewType) {
            case CHAT_ITEM_TYPE_LEFT:{
                viewHolder = new ChatAcceptViewHolder(parent, onItemClickListener, handler);
                break;
            } case CHAT_ITEM_TYPE_RIGHT:{
                viewHolder = new ChatSendViewHolder(parent, onItemClickListener, handler);
                break;
            }
        }
        return viewHolder;
    }

    @Override
    public void onBindViewHolder(@NonNull BaseViewHolder holder, int position) {
        holder.itemView.setTag(position);
        holder.setData(messageInfoList.get(position));
    }

    @Override
    public int getItemCount() {
        return CollectionUtils.isEmpty(messageInfoList) ? 0 : messageInfoList.size();
    }

    public int getItemViewType(int poistion) {
        return messageInfoList.get(poistion).getType();
    }

    public void addAll(List<MessageInfo> messageInfos) {
        if (CollectionUtils.isEmpty(messageInfoList)) {
            this.messageInfoList = messageInfos;
        } else {
            messageInfoList.addAll(messageInfos);
        }
        notifyDataSetChanged();
    }

    public void add(MessageInfo messageInfo) {
        if (CollectionUtils.isEmpty(messageInfoList)) {
            this.messageInfoList = new ArrayList<MessageInfo>();
        }
        this.messageInfoList.add(messageInfo);
        notifyDataSetChanged();
    }

    public interface OnItemClickListener {
        void onHeaderClick(int position);

        void onImageClick(View view, int position);

        void onVoiceClick(ImageView imageView, int position);

        void onFileClick(View view, int position);

        void onLinkClick(View view, int position);

        void onLongClickImage(View view, int position);

        void onLongClickText(View view, int position);

        void onLongClickItem(View view, int position);

        void onLongClickFile(View view, int position);

        void onLongClickLink(View view, int position);
    }
}